package com.lc.user.express.my;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.lc.user.express.BaseSecondActivity;
import com.lc.user.express.R;
import com.lc.user.express.adapter.AccountDetailsAdapter;
import com.lc.user.express.alipay.PayDemoActivity;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.httpserver.GetBalanceDetail;
import com.lc.user.express.model.AccountDetailsModel;
import com.lc.user.express.view.pull.PullToRefreshBase;
import com.lc.user.express.view.pull.PullToRefreshListView;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/9.账户余额
 */
public class AccountBalanceActivity extends BaseSecondActivity {
    public static OnRefreshData onRefreshData = null;

    public interface OnRefreshData {
        void setData();
    }

    private TextView tv_amount;
    private PullToRefreshListView plv;
    private AccountDetailsAdapter adapter;
    private List<AccountDetailsModel.ListBean> list = new ArrayList<>();
    private int page = 1;
    private int ispage;
    private int nextpage;
    private GetBalanceDetail getBalanceDetail = new GetBalanceDetail(AppContext.myPreferences.getUid(), page, new AsyCallBack<AccountDetailsModel>() {
        @Override
        public void onSuccess(String toast, int type, AccountDetailsModel info) throws Exception {
            tv_amount.setText(info.getAccount() + "元");
            if (page == 1) {
                list.clear();
            }
            list.addAll(info.getList());
            adapter.updateListView(list);
            ispage = Integer.valueOf(info.getIspage());
            nextpage = Integer.valueOf(info.getNextpage());
            plv.onPullDownRefreshComplete();
            plv.onPullUpRefreshComplete();
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_balance);
        initView();
        getBalanceDetail(1, true);
    }

    private void initView() {
        setTitle("账户余额");

        onRefreshData = new OnRefreshData() {
            @Override
            public void setData() {
                getBalanceDetail.execute(cnt, true);
            }
        };
        tv_amount = (TextView) findViewById(R.id.tv_amount);
        ((TextView) findViewById(R.id.tv_public)).setOnClickListener(this);

        plv = (PullToRefreshListView) findViewById(R.id.lv);
        plv.setPullLoadEnabled(false);
        plv.setScrollLoadEnabled(true);
        plv.setPullRefreshEnabled(true);
        plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                getBalanceDetail(1, false);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                if (ispage == 1) {
                    getBalanceDetail(nextpage, false);
                } else {
                    try {
                        plv.onPullDownRefreshComplete();
                        plv.onPullUpRefreshComplete();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        ListView lv = plv.getRefreshableView();
        lv.setDivider(null);
        adapter = new AccountDetailsAdapter(cnt, list);
        lv.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_public:
                show(PayDemoActivity.class);
                break;
        }
    }

    private void getBalanceDetail(int page, boolean isShow) {
        getBalanceDetail.page = page;
        getBalanceDetail.execute(cnt, isShow);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (MyFragment.onReFresh != null) {
            MyFragment.onReFresh.onRefresh();
        }
    }
}
